﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TV.Translation.Library.Base;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Data;

namespace TV.Translation.Library.Harris
{
    [Serializable]
    public class HSeriesLog : BroadcastLog
    {

        protected XmlDocument originalXml;

        [XmlElement(ElementName="dlevent")]
        public HSeriesEvent[] HEvents
        {
            get
            {
                HSeriesEvent[] dlevents = new HSeriesEvent[m_events.Count];
                for (int i = 0; i < m_events.Count; i++)
                {
                    dlevents[i] = (HSeriesEvent)m_events.ElementAt(i).Value;
                }
                return dlevents;
            }
            set
            {
                if (null == value)
                    return;

                HSeriesEvent[] dlevents = (HSeriesEvent[])value;
                m_events.Clear();
                foreach (HSeriesEvent dlevent in dlevents)
                {
                    m_events.Add(dlevent.Start, dlevent);
                }
            }
        }

        public override void Load(string fullLogPath)
        {
            if (String.IsNullOrEmpty(fullLogPath))
            {
                return;
            }

            DateTime startTime = DateTime.Now;

            originalXml = new XmlDocument();
            TextReader r = new StreamReader(fullLogPath);
            XmlTextReader xmlReader = new XmlTextReader(r);
            originalXml.Load(xmlReader);

            #region Populate Events

            XmlNodeList eventNodes = originalXml.GetElementsByTagName("dlevent");
            DataTable eventsTable = new DataTable();
            eventsTable.Columns.Add("eventid");
            eventsTable.Columns.Add("plobjid");
            eventsTable.Columns.Add("sevtype");
            foreach (XmlNode infoNode in eventNodes.Item(2).ChildNodes)
            {
                if (!eventsTable.Columns.Contains(infoNode.Name))
                {
                    eventsTable.Columns.Add(infoNode.Name);
                }
            }

            foreach (XmlNode eventNode in eventNodes)
            {
                DataRow row = eventsTable.NewRow();
                row.SetField<String>("eventid", eventNode.Attributes["eventid"].Value);
                row.SetField<String>("plobjid", eventNode.Attributes["plobjid"].Value);
                row.SetField<String>("sevtype", eventNode.Attributes["sevtype"].Value);
                foreach (XmlNode infoNode in eventNode.ChildNodes)
                {
                    if (eventsTable.Columns.Contains(infoNode.Name))
                    {
                        row.SetField<String>(infoNode.Name, infoNode.InnerText);
                    }
                }
                eventsTable.Rows.Add(row);
            }

            XmlNodeList dlplobjNodes = originalXml.GetElementsByTagName("dlplobj");
            DataTable dlplobjTable = new DataTable();
            dlplobjTable.Columns.Add("plobjid");
            foreach (XmlNode infoNode in dlplobjNodes.Item(0).ChildNodes)
            {
                if (!dlplobjTable.Columns.Contains(infoNode.Name))
                {
                    dlplobjTable.Columns.Add(infoNode.Name);
                }
            }

            foreach (XmlNode dlplobjNode in dlplobjNodes)
            {
                DataRow row = dlplobjTable.NewRow();
                row.SetField<String>("plobjid", dlplobjNode.Attributes["plobjid"].Value);
                foreach (XmlNode infoNode in dlplobjNode.ChildNodes)
                {
                    if (dlplobjTable.Columns.Contains(infoNode.Name))
                    {
                        row.SetField<String>(infoNode.Name, infoNode.InnerText);
                    }
                }
                dlplobjTable.Rows.Add(row);
            }

            DataSet playlistLogDataSet = new DataSet();
            playlistLogDataSet.Tables.Add(eventsTable);
            playlistLogDataSet.Tables.Add(dlplobjTable);

            int numOfEvents = eventsTable.Rows.Count;
            SortedList<DateTimeFrames, HSeriesEvent> secondaryEvents = new SortedList<DateTimeFrames, HSeriesEvent>();
            HSeriesEvent lastPrimaryEvent = null;

            for (int i = 0; i < numOfEvents; i++)
            {
                String currPlobjId = eventsTable.Rows[i].Field<String>("plobjid");
                DataRow relatedPlobjObject = dlplobjTable.Select("plobjid = '" + currPlobjId + "'")[0];

                HSeriesEvent currEvent = new HSeriesEvent();
                currEvent.EventID = currPlobjId;
                currEvent.Start.setValue(eventsTable.Rows[i].Field<String>("dlstart"));
                currEvent.Dlhnumber = eventsTable.Rows[i].Field<String>("dlhnumber");
                currEvent.Duration.setValue(eventsTable.Rows[i].Field<String>("dlduration_hmsf"));
                currEvent.Sevtype = eventsTable.Rows[i].Field<String>("sevtype");
                currEvent.Spottype = eventsTable.Rows[i].Field<String>("spottype");


                if (0 == i)
                {
                    lastPrimaryEvent = currEvent;
                }
                else
                {
                    if (currEvent.Type == EventType.Primary)
                    {
                        if (secondaryEvents.Count > 0)
                        {
                            if (null == lastPrimaryEvent.SecondaryEvents)
                            {
                                lastPrimaryEvent.initializeSecondaryEvents();
                            }
                            for (int j = 0; j < secondaryEvents.Count; j++)
                            {
                                lastPrimaryEvent.SecondaryEvents.Add(secondaryEvents.ElementAt(j).Key, secondaryEvents.ElementAt(j).Value);
                            }
                            secondaryEvents.Clear();
                        }
                        m_events.Add(lastPrimaryEvent.Start, lastPrimaryEvent);
                        lastPrimaryEvent = currEvent;
                    }
                    else
                    {
                        secondaryEvents.Add(currEvent.Start, currEvent);
                    }
                        
                }


            }



            

            //foreach (XmlNode eventNode in eventNodes)
            //{
            //    HSeriesEvent hEvent = new HSeriesEvent();
            //    hEvent.EventID = eventNode.Attributes["eventid"].Value;
            //    XmlNodeList eventProperties = eventNode.ChildNodes;

            //    foreach (XmlNode eventProperty in eventProperties)
            //    {
            //        if (eventProperty.Name.Equals("dlstart"))
            //        {
            //            String eventStartTime = eventProperty.InnerText;
            //            if (!String.IsNullOrEmpty(eventStartTime))
            //            {
            //                hEvent.StartTime = eventStartTime;
            //            }
            //        }
            //        else if (eventProperty.Name.Equals("dlhnumber"))
            //        {
            //            String dlhnumber = eventProperty.InnerText;
            //            if (!String.IsNullOrEmpty(dlhnumber))
            //            {
            //                hEvent.Dlhnumber = dlhnumber;
            //            }
            //        }
            //    }

            //    m_events.Add(hEvent.Start, hEvent);

            //}


            #endregion Populate Events

            DateTime endTime = DateTime.Now;
            TimeSpan totalDuration = endTime.Subtract(startTime);

        }

    }
}
